package com.myamap.tool;

import java.util.List;

import org.CrossApp.lib.CrossAppActivity;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.amap.api.services.poisearch.PoiSearch.OnPoiSearchListener;
import com.amap.api.services.poisearch.PoiSearch.SearchBound;

public class AppTool
{
	private static AMapLocationClient mLocationClient = null;
	
	native static void onOnceLocationEnd(int code, double lat, double lng, String formatAddr, String province, String city, 
			String district, String street, String number, String poi, String adcode);
	//单次定位
	public static void startOnceLocation()
	{
		if (mLocationClient == null)
		{
			mLocationClient = new AMapLocationClient(CrossAppActivity.getContext());
		}
		
		AMapLocationClientOption locationOption = new AMapLocationClientOption();
		locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
		locationOption.setOnceLocation(true);
		locationOption.setNeedAddress(true);
		mLocationClient.setLocationOption(locationOption);
		mLocationClient.setLocationListener(new AMapLocationListener() 
		{
			@Override
			public void onLocationChanged(AMapLocation location) 
			{
				// TODO Auto-generated method stub
				int code = 0;
				double lat = 0.0;
				double lng = 0.0;
				String formatAddr = ""; 
				String province = ""; 
				String city = "";
				String district = "";
				String street = ""; 
				String number = "";
				String poi = ""; 
				String adcode = "";
				if (location != null && location.getErrorCode() == 0) 
				{
					code = 0;
					lat = location.getLatitude();
					lng = location.getLongitude();
					
					formatAddr = location.getAddress(); 
					province = location.getProvince(); 
					city = location.getCity();
					district = location.getDistrict();
					street = location.getStreet(); 
					number = location.getStreetNum();
					poi = location.getPoiName(); 
					adcode = location.getAdCode();
				}
				else 
				{
					code = -1;
					if (location != null)
					{
						code = location.getErrorCode();
					}
				}
				
				mLocationClient.stopLocation();
				
				onOnceLocationEnd(code, lat, lng, formatAddr, province, city, district, street, number, poi, adcode);
			}
		});
		
		mLocationClient.startLocation();
	}
	
	native static void onLocationUpdate(int code, double lat, double lng, String formatAddr, String province, String city, 
			String district, String street, String number, String poi, String adcode);
	//持续定位
	public static void startLocation(int interval)
	{
		if (mLocationClient == null)
		{
			mLocationClient = new AMapLocationClient(CrossAppActivity.getContext());
		}
		
		AMapLocationClientOption locationOption = new AMapLocationClientOption();
		locationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
		locationOption.setInterval(interval);
		locationOption.setNeedAddress(true);
		mLocationClient.setLocationOption(locationOption);
		mLocationClient.setLocationListener(new AMapLocationListener() 
		{
			@Override
			public void onLocationChanged(AMapLocation location) 
			{
				// TODO Auto-generated method stub
				int code = 0;
				double lat = 0.0;
				double lng = 0.0;
				String formatAddr = ""; 
				String province = ""; 
				String city = "";
				String district = "";
				String street = ""; 
				String number = "";
				String poi = ""; 
				String adcode = "";
				if (location != null && location.getErrorCode() == 0) 
				{
					code = 0;
					lat = location.getLatitude();
					lng = location.getLongitude();
					
					formatAddr = location.getAddress(); 
					province = location.getProvince(); 
					city = location.getCity();
					district = location.getDistrict();
					street = location.getStreet(); 
					number = location.getStreetNum();
					poi = location.getPoiName(); 
					adcode = location.getAdCode();
				}
				else 
				{
					code = -1;
					if (location != null)
					{
						code = location.getErrorCode();
					}
				}
				
				onLocationUpdate(code, lat, lng, formatAddr, province, city, district, street, number, poi, adcode);
			}
		});
		
		mLocationClient.startLocation();
	}
	
	public static void stopLocation()
	{
		if (mLocationClient != null)
		{
			mLocationClient.stopLocation();
		}
	}
	
	native static void onGetGeocodeAddress(int code, double[] latArray, double[] lngArray, Object[] formataddrArray, Object[] provinceArray, 
			Object[] cityArray, Object[] districtArray, Object[] townArray, Object[] streetArray, Object[] adcodeArray);
	//正地理编码
	public static void getGeoCode(String addr, String city)
	{
		GeocodeSearch geocoderSearch = new GeocodeSearch(CrossAppActivity.getContext());
		geocoderSearch.setOnGeocodeSearchListener(new OnGeocodeSearchListener()
		{
			@Override
			public void onGeocodeSearched(GeocodeResult result, int rCode)
			{
				int code = 0;
				// TODO Auto-generated method stub
				if (rCode == AMapException.CODE_AMAP_SUCCESS)
				{
					if (result != null && result.getGeocodeAddressList() != null
							&& result.getGeocodeAddressList().size() > 0) 
					{
						int size = result.getGeocodeAddressList().size();
						double[] latArray = new double[size];
						double[] lngArray = new double[size];
						String[] formataddrArray = new String[size];
						String[] provinceArray = new String[size];
						String[] cityArray = new String[size];
						String[] districtArray = new String[size];
						String[] townArray = new String[size];
						String[] streetArray = new String[size];
						String[] adcodeArray = new String[size];
						
						for (int i = 0; i < size; ++i)
						{
							GeocodeAddress address = result.getGeocodeAddressList().get(i);
							
							latArray[i] = address.getLatLonPoint().getLatitude();
							lngArray[i] = address.getLatLonPoint().getLongitude();
							formataddrArray[i] = address.getFormatAddress();
							provinceArray[i] = address.getProvince();
							cityArray[i] = address.getCity();
							districtArray[i] = address.getDistrict();
							townArray[i] = address.getTownship();
							streetArray[i] = address.getNeighborhood();
							adcodeArray[i] = address.getAdcode();
						}
						
						onGetGeocodeAddress(code, latArray, lngArray, formataddrArray, provinceArray, cityArray, districtArray, townArray, streetArray, adcodeArray);
					}
					else
					{
						code = -1;
						onGetGeocodeAddress(code, null, null, null, null, null, null, null, null, null);
					}
				} 
				else 
				{
					code = -1;
					onGetGeocodeAddress(code, null, null, null, null, null, null, null, null, null);
				}
			}

			@Override
			public void onRegeocodeSearched(RegeocodeResult arg0, int arg1)
			{
				// TODO Auto-generated method stub
			}
			
		});
		GeocodeQuery query = new GeocodeQuery(addr, city);// 第一个参数表示地址，第二个参数表示查询城市，中文或者中文全拼，citycode、adcode，
		geocoderSearch.getFromLocationNameAsyn(query);// 设置地理编码请求
	}
	
	native static void onGetRegeocodeInfo(int code, String formatAddr, String province, String city, String district, String town, String street, 
			String adcode, double[] poilatArray, double[] poilngArray, Object[] poiInfoArray);
	//反地理编码
	public static void getReGeoCode(double lat, double lng, float radius)
	{
		GeocodeSearch geocoderSearch = new GeocodeSearch(CrossAppActivity.getContext());
		geocoderSearch.setOnGeocodeSearchListener(new OnGeocodeSearchListener()
		{

			@Override
			public void onGeocodeSearched(GeocodeResult arg0, int arg1)
			{
				// TODO Auto-generated method stub	
			}

			@Override
			public void onRegeocodeSearched(RegeocodeResult result, int rCode)
			{
				// TODO Auto-generated method stub
				int code = 0;
				// TODO Auto-generated method stub
				if (rCode == AMapException.CODE_AMAP_SUCCESS)
				{
					if (result != null && result.getRegeocodeAddress() != null
							&& result.getRegeocodeAddress().getFormatAddress() != null) 
					{
						String formatAddr = result.getRegeocodeAddress().getFormatAddress();
						String province = result.getRegeocodeAddress().getProvince();
						String city = result.getRegeocodeAddress().getCity();
						String district = result.getRegeocodeAddress().getDistrict();
						String town = result.getRegeocodeAddress().getTownship();
						String street = result.getRegeocodeAddress().getNeighborhood();
						String adcode = result.getRegeocodeAddress().getAdCode();
						
						int size = result.getRegeocodeAddress().getPois() != null ? result.getRegeocodeAddress().getPois().size() : 0;
						
						double[] poilatArray = new double[size];
						double[] poilngArray = new double[size];
						String[] poiinfoArray = new String[size];
						for (int i = 0; i < size; ++i)
						{
							PoiItem poiItem = result.getRegeocodeAddress().getPois().get(i);
							
							poilatArray[i] = poiItem.getLatLonPoint().getLatitude();
							poilngArray[i] = poiItem.getLatLonPoint().getLongitude();
							poiinfoArray[i] = poiItem.getTitle();
						}
						
						onGetRegeocodeInfo(code, formatAddr, province, city, district, town, street, adcode, poilatArray, poilngArray, poiinfoArray);
					}
					else
					{
						code = -1;
						onGetRegeocodeInfo(code, "", "", "", "", "", "", "", null, null, null);
					}
				}
				else
				{
					code = -1;
					onGetRegeocodeInfo(code, "", "", "", "", "", "", "", null, null, null);
				}
			}
			
		});
		RegeocodeQuery query = new RegeocodeQuery(new LatLonPoint(lat, lng), radius, GeocodeSearch.AMAP);// 第一个参数表示地址，第二个参数表示查询城市，中文或者中文全拼，citycode、adcode，
		geocoderSearch.getFromLocationAsyn(query);// 设置地理编码请求
	}
	
	private static PoiSearch mPoiSearch = null;
	native static void onPoiKeySearch(int code, double[] latArray, double[] lngArray, Object[] provinceArray, 
			Object[] cityArray, Object[] districtArray, Object[] adcodeArray, Object[] infoArray);
	public static void poiKeySearch(String keystr, String poitype, String city)
	{
		if (mPoiSearch == null)
		{
			mPoiSearch = new PoiSearch(CrossAppActivity.getContext(), null);
		}
		
		mPoiSearch.setOnPoiSearchListener(new OnPoiSearchListener() 
		{
			@Override
			public void onPoiSearched(PoiResult result, int rCode) 
			{
				// TODO Auto-generated method stub
				if (rCode == AMapException.CODE_AMAP_SUCCESS) 
				{
					if (result != null && result.getQuery() != null) 
					{
						int size = result.getPois().size();
						
						double[] latArray = new double[size];
						double[] lngArray = new double[size];
						String[] provinceArray = new String[size];
						String[] cityArray = new String[size];
						String[] districtArray = new String[size];
						String[] adcodeArray = new String[size];
						String[] infoArray = new String[size];
						
						for(int i = 0; i < size; ++i)
						{
							PoiItem poiItem = result.getPois().get(i);
							
							latArray[i] = poiItem.getLatLonPoint().getLatitude();
							lngArray[i] = poiItem.getLatLonPoint().getLongitude();
							provinceArray[i] = poiItem.getProvinceName();
							cityArray[i] = poiItem.getCityName();
							districtArray[i] = poiItem.getAdName();
							adcodeArray[i] = poiItem.getAdCode();
							infoArray[i] = poiItem.getTitle();
						}
						
						onPoiKeySearch(0, latArray, lngArray, provinceArray, cityArray, districtArray, adcodeArray, infoArray);
					}
					else
					{
						onPoiKeySearch(-1, null, null, null, null, null, null, null);
					}
				}
				else
				{
					onPoiKeySearch(-1, null, null, null, null, null, null, null);
				}
			}
			
			@Override
			public void onPoiItemSearched(PoiItem arg0, int arg1)
			{
				// TODO Auto-generated method stub
				
			}
		});
		
		PoiSearch.Query query = new PoiSearch.Query(keystr, poitype, city);// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
		query.setPageSize(50);// 设置每页最多返回多少条poiitem
		query.setPageNum(0);// 设置查第一页
		if (city.equals(""))
		{
			query.setCityLimit(false);
		}
		else
		{
			query.setCityLimit(true);
		}
		
		mPoiSearch.setQuery(query);
		mPoiSearch.searchPOIAsyn();
	}
	
	native static void onPoiAroundSearch(int code, double[] latArray, double[] lngArray, Object[] provinceArray, 
			Object[] cityArray, Object[] districtArray, Object[] adcodeArray, Object[] infoArray);
	public static void poiAroundSearch(String keystr, String poitype, double lat, double lng, int round, String city)
	{
		if (mPoiSearch == null)
		{
			mPoiSearch = new PoiSearch(CrossAppActivity.getContext(), null);
		}
		
		mPoiSearch.setOnPoiSearchListener(new OnPoiSearchListener() 
		{
			@Override
			public void onPoiSearched(PoiResult result, int rCode) 
			{
				// TODO Auto-generated method stub
				if (rCode == AMapException.CODE_AMAP_SUCCESS) 
				{
					if (result != null && result.getQuery() != null) 
					{
						int size = result.getPois().size();
						
						double[] latArray = new double[size];
						double[] lngArray = new double[size];
						String[] provinceArray = new String[size];
						String[] cityArray = new String[size];
						String[] districtArray = new String[size];
						String[] adcodeArray = new String[size];
						String[] infoArray = new String[size];
						
						for(int i = 0; i < size; ++i)
						{
							PoiItem poiItem = result.getPois().get(i);
							
							latArray[i] = poiItem.getLatLonPoint().getLatitude();
							lngArray[i] = poiItem.getLatLonPoint().getLongitude();
							provinceArray[i] = poiItem.getProvinceName();
							cityArray[i] = poiItem.getCityName();
							districtArray[i] = poiItem.getAdName();
							adcodeArray[i] = poiItem.getAdCode();
							infoArray[i] = poiItem.getTitle();
						}
						
						onPoiAroundSearch(0, latArray, lngArray, provinceArray, cityArray, districtArray, adcodeArray, infoArray);
					}
					else
					{
						onPoiAroundSearch(-1, null, null, null, null, null, null, null);
					}
				}
				else
				{
					onPoiAroundSearch(-1, null, null, null, null, null, null, null);
				}
			}
			
			@Override
			public void onPoiItemSearched(PoiItem arg0, int arg1)
			{
				// TODO Auto-generated method stub
				
			}
		});
		
		PoiSearch.Query query = new PoiSearch.Query(keystr, poitype, city);// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
		query.setPageSize(50);// 设置每页最多返回多少条poiitem
		query.setPageNum(0);// 设置查第一页
		if (city.equals(""))
		{
			query.setCityLimit(false);
		}
		else
		{
			query.setCityLimit(true);
		}
		
		if (lat != -1000 && lng != -1000)
		{
			mPoiSearch.setBound(new SearchBound(new LatLonPoint(lat, lng), round, true));
		}
		
		mPoiSearch.setQuery(query);
		mPoiSearch.searchPOIAsyn();
	}
}